package com.wink_172.activity

import android.animation.Animator
import android.animation.ObjectAnimator
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.os.Message
import android.util.Log
import android.view.ViewGroup
import android.widget.LinearLayout
import androidx.databinding.DataBindingUtil
import com.wink_172.commons.R
import com.wink_172.commons.databinding.ActivityAnimBinding
import com.wink_172.commons.databinding.ActivityControlBinding
import com.wink_172.library.activity.SmartActivity
import com.wink_172.library.utils.DisplayUtil
import org.xutils.x

/**
 * 动画合集
 * */
class AnimActivity : SmartActivity() {

    lateinit var binding: ActivityAnimBinding

    companion object {
        @JvmStatic
        fun startActivity(context: Context, vararg args: Any?) {
            val intent = Intent(context, AnimActivity::class.java)

            //intent.putExtra(Constants.PARAM_DATA1, (String) args[0]);
            context.startActivity(intent)
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = DataBindingUtil.setContentView(this, R.layout.activity_anim)


        initView()
        init()



        all_count = binding.container3.childCount
        current_pos = 0

        getHandle().removeMessages(eventv1)
        getHandle().sendEmptyMessageDelayed(eventv1, 1000)
    }

    fun initView() {

    }


    var all_count = -1
    var current_pos = -1

    val eventv1 = 3

    override fun handlerCallBack(msg: Message?) {
        super.handlerCallBack(msg)
        when (msg!!.what) {
            eventv1 -> {
                testAnimv1()
                current_pos +=1
                if (current_pos == all_count ) {
                    current_pos = 0
                }
                getHandle().removeMessages(eventv1)
                getHandle().sendEmptyMessageDelayed(eventv1, 5000)
            }



        }
    }

    override fun onDestroy() {
        super.onDestroy()
        getHandle().removeCallbacksAndMessages(null)
    }
    fun init() {
        val animator = ObjectAnimator.ofFloat(binding!!.anim1, "alpha", 0.1f, 1f)//可以往后面连续追加数值
        animator.duration = 2000
        animator.start()

        val animator2 = ObjectAnimator.ofFloat(binding!!.anim2, "scaleY", 1f, 2f)//可以往后面连续追加数值
        animator2.duration = 2000
        animator2.start()

        val animator4 = ObjectAnimator.ofFloat(binding!!.anim3, "scaleX", 1f, 2f)//可以往后面连续追加数值
        animator4.duration = 2000
        animator4.start()

        val curTranslationX: Float = binding!!.anim4!!.getTranslationX()
        var animator3 = ObjectAnimator.ofFloat(
            binding!!.anim4,
            "translationX",
            curTranslationX,
            200f,
            300f
        )//可以往后面连续追加数值


        animator3.duration = 2000L
        animator3.start()


    }


    val move_x = DisplayUtil.dp2px(x.app(), 100f)


    fun testAnimv1() {
        Log.e(TAG, "testAnimv1: ====>>" + current_pos)
        val viewv1 = binding.container3.getChildAt(current_pos)
        var viewv2 = binding.container3.getChildAt(current_pos + 1)
        for (i in 0..binding.container3.childCount - 1) {
          val item_view= binding.container3.getChildAt(i)
            item_view.alpha=0f
            if(i==current_pos){
                item_view.alpha=1f
            }else{

            }

        }

        val animator_alph1 = ObjectAnimator.ofFloat(viewv1, "alpha", 1f, 0f)//可以往后面连续追加数值
        animator_alph1.duration = 1000
        animator_alph1.start()

        viewv1.translationX = 0f
        var animator111 = ObjectAnimator.ofFloat(viewv1, "translationX", viewv1.translationX, -move_x.toFloat())//可以往后面连续追加数值
        animator111.duration = 1000L
        animator111.start()


        //一轮反转
        if(viewv2==null){
             viewv2 = binding.container3.getChildAt(0)
        }
        val animator_alph2 = ObjectAnimator.ofFloat(viewv2, "alpha", 0f, 1f)//可以往后面连续追加数值
        animator_alph2.duration = 1000
        animator_alph2.start()


        viewv2.translationX = 0f + move_x
        var animator112 = ObjectAnimator.ofFloat(
            viewv2,
            "translationX",
            viewv2.translationX,
           0f
        )//可以往后面连续追加数值
        animator112.duration = 1000L

        animator112.start()


    }



    private val TAG = "AnimActivity"
}